@{
    ViewData["Title"] = "销售订单管理";
    Layout = null;
}

<!-- 直接引入所有必要的CSS和JS文件 -->
<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>

<style>
    body {
        background: #f4f6fa;
        font-family: 'Segoe UI', 'Microsoft YaHei', Arial, sans-serif;
    }
    .order-container {
        padding: 32px 0;
        background-color: #f4f6fa;
        min-height: 100vh;
    }
    .search-area {
        background: #fff;
        padding: 28px 28px 8px 28px;
        border-radius: 12px;
        box-shadow: 0 4px 24px rgba(30, 159, 255, 0.08), 0 1.5px 6px rgba(0,0,0,0.03);
        margin-bottom: 24px;
        border: 1px solid #e6eaf0;
        max-width: 1200px;
        margin-left: auto;
        margin-right: auto;
    }
    .search-form .layui-form-item {
        margin-bottom: 18px;
    }
    .search-form .layui-inline {
        margin-right: 18px;
        margin-bottom: 10px;
    }
    .search-form .layui-input-inline {
        width: 200px;
    }
    .search-form .layui-btn {
        margin-right: 10px;
        border-radius: 6px;
        font-size: 15px;
        font-weight: 500;
        padding: 0 22px;
    }
    .search-label {
        display: inline-block;
        width: 90px;
        text-align: right;
        padding-right: 10px;
        color: #1e9fff;
        font-weight: 500;
        font-size: 15px;
    }
    .layui-input, .layui-select {
        border-radius: 6px !important;
        border: 1px solid #e6eaf0;
        font-size: 15px;
        background: #f9fbfd;
        transition: border-color 0.2s;
    }
    .layui-input:focus, .layui-select:focus {
        border-color: #1e9fff;
        background: #fff;
    }
    .action-buttons {
        margin: 18px 0 10px 0;
        text-align: left;
    }
    .action-buttons .layui-btn {
        margin-right: 12px;
        border-radius: 6px;
        font-size: 15px;
        font-weight: 500;
        padding: 0 22px;
        box-shadow: 0 2px 8px rgba(30,159,255,0.08);
    }
    .layui-table {
        margin-top: 10px;
        border-radius: 10px;
        overflow: hidden;
        border: 1px solid #e6eaf0;
        background: #fff;
        font-size: 15px;
    }
    .layui-table thead tr {
        background: linear-gradient(90deg, #e3f2fd 0%, #f4f6fa 100%);
    }
    .layui-table th, .layui-table td {
        font-size: 15px;
        color: #333;
        padding: 10px 8px;
        border-color: #e6eaf0;
    }
    .layui-table th {
        font-weight: 600;
        color: #1e9fff;
        background: #f4f6fa;
    }
    .layui-table-tool {
        background-color: #f7f7f7;
    }
    .table-operations .layui-btn+.layui-btn {
        margin-left: 5px;
    }
    .layui-table-page {
        text-align: right;
    }
    /* 状态显示样式 */
    .status-tag {
        display: inline-block;
        padding: 2px 10px;
        border-radius: 12px;
        font-size: 13px;
        font-weight: 500;
        margin-right: 2px;
    }
    .status-tag-normal {
        background-color: #e8f4ff;
        color: #1890ff;
    }
    .status-tag-warning {
        background-color: #fffbe6;
        color: #faad14;
    }
    .status-tag-danger {
        background-color: #fff1f0;
        color: #f5222d;
    }
    .status-tag-success {
        background-color: #f6ffed;
        color: #52c41a;
    }
    /* 响应式优化 */
    @@media (max-width: 1200px) {
        .search-area {
            max-width: 100%;
            padding: 16px 4px 4px 4px;
        }
    }
    @@media (max-width: 800px) {
        .order-container {
            padding: 8px 0;
        }
        .search-label {
            width: 60px;
            font-size: 13px;
        }
        .search-form .layui-input-inline {
            width: 120px;
        }
    }
</style>

<div class="order-container">
    <!-- 搜索区域 -->
    <div class="search-area">
        <form class="layui-form search-form" lay-filter="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="search-label">订单编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderCode" placeholder="请输入订单编码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">订单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderName" placeholder="请输入订单名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">订单状态</label>
                    <div class="layui-input-inline">
                        <select name="orderCategory">
                            <option value="">请选择订单状态</option>
                            <option value="生产中">生产中</option>
                            <option value="单据">单据</option>
                            <option value="已完成">已完成</option>
                            <option value="已确认">已确认</option>
                            <option value="已发货">已发货</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">客户编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="customerCode" placeholder="请输入客户编码" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="search-label">客户PO号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="customerPONumber" placeholder="请输入客户PO号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">客户名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="customerName" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">订货日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderDateStart" id="orderDateStart" placeholder="开始日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 180px;">
                        <input type="text" name="orderDateEnd" id="orderDateEnd" placeholder="结束日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="search-label">交货日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="deliveryDateStart" id="deliveryDateStart" placeholder="开始日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 180px;">
                        <input type="text" name="deliveryDateEnd" id="deliveryDateEnd" placeholder="结束日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">币别</label>
                    <div class="layui-input-inline">
                        <select name="currency">
                            <option value="">请选择币别</option>
                            <option value="RMB">人民币</option>
                            <option value="USD">美元</option>
                            <option value="EUR">欧元</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">结账方式</label>
                    <div class="layui-input-inline">
                        <select name="settlementMethod">
                            <option value="">请选择结账方式</option>
                            <option value="月结30天">月结30天</option>
                            <option value="月结45天">月结45天</option>
                            <option value="月结60天">月结60天</option>
                            <option value="转账">转账</option>
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="search-label">订单类别</label>
                    <div class="layui-input-inline">
                        <select name="orderType">
                            <option value="">请选择订单类别</option>
                            <option value="客户订单">客户订单</option>
                            <option value="备货订单">备货订单</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">跟单</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderFollower" placeholder="请输入跟单人员" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">业务员</label>
                    <div class="layui-input-inline">
                        <input type="text" name="salesperson" placeholder="请输入业务员" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="search-label">质量要求</label>
                    <div class="layui-input-inline">
                        <select name="qualityRequirement">
                            <option value="">请选择质量要求</option>
                            <option value="AAA">AAA</option>
                            <option value="AA">AA</option>
                            <option value="A">A</option>
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block" style="margin-left: 0; text-align: center;">
                    <button class="layui-btn" lay-submit lay-filter="searchBtn">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </form>
    </div>
    
    <!-- 操作按钮 -->
    <div class="action-buttons">
        <button class="layui-btn layui-btn-sm" id="addBtn">
            <i class="layui-icon layui-icon-add-1"></i> 新增
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" id="editBtn">
            <i class="layui-icon layui-icon-edit"></i> 修改
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" id="deleteBtn">
            <i class="layui-icon layui-icon-delete"></i> 删除
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-primary" id="exportBtn">
            <i class="layui-icon layui-icon-export"></i> 导出
        </button>
    </div>
    
    <!-- 表格区域 -->
    <table class="layui-hide" id="salesOrderTable" lay-filter="salesOrderTable"></table>
</div>

<!-- 操作列模板 -->
<script type="text/html" id="operationTpl">
    <div class="table-operations">
        <button class="layui-btn layui-btn-xs" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </div>
</script>

<!-- 状态模板 -->
<script type="text/html" id="statusTpl">
    {{# if(d.dOrderCategory === '单据'){ }}
    <span class="status-tag status-tag-normal">单据</span>
    {{# } else if(d.dOrderCategory === '已完成'){ }}
    <span class="status-tag status-tag-success">已完成</span>
    {{# } else if(d.dOrderCategory === '生产中'){ }}
    <span class="status-tag status-tag-warning">生产中</span>
    {{# } else if(d.dOrderCategory === '已发货'){ }}
    <span class="status-tag status-tag-success">已发货</span>
    {{# } else if(d.dOrderCategory === '已确认'){ }}
    <span class="status-tag status-tag-normal">已确认</span>
    {{# } else { }}
    <span class="status-tag">{{d.dOrderCategory}}</span>
    {{# } }}
</script>

<!-- 添加销售订单对话框 -->
<div id="addOrderDialog" style="display: none; padding: 20px;">
    <form class="layui-form" id="orderForm" lay-filter="orderForm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">* 订单编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="dOrderCode" placeholder="请输入订单编码" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
                <div class="layui-form-mid">
                    <input type="checkbox" name="autoGenerate" title="自动生成" lay-filter="autoGenerate">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="dOrderName" placeholder="请输入订单名称" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="dCustomerCode" placeholder="请选择客户" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="selectCustomerBtn">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">* 客户名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="dCustomerName" placeholder="客户名称" autocomplete="off" class="layui-input" lay-verify="required" readonly>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户PO号</label>
                <div class="layui-input-inline">
                    <input type="text" name="dCustomerPONumber" placeholder="请输入客户PO号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">* 订货日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="dOrderDate" id="orderDate" placeholder="请选择订货日期" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">* 交货日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="deliveryDate" id="deliveryDate" placeholder="请选择交货日期" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">币别</label>
                <div class="layui-input-inline">
                    <select name="dCurrency">
                        <option value="">请选择币别</option>
                        <option value="RMB">人民币</option>
                        <option value="USD">美元</option>
                        <option value="EUR">欧元</option>
                    </select>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">结账方式</label>
                <div class="layui-input-inline">
                    <select name="dSettlementMethod">
                        <option value="">请选择结账方式</option>
                        <option value="月结30天">月结30天</option>
                        <option value="月结45天">月结45天</option>
                        <option value="月结60天">月结60天</option>
                        <option value="转账">转账</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">* 订单类别</label>
                <div class="layui-input-inline">
                    <select name="dOrderCategory" lay-verify="required">
                        <option value="">请选择订单类别</option>
                        <option value="客户订单">客户订单</option>
                        <option value="备货订单">备货订单</option>
                        <option value="单据">单据</option>
                        <option value="生产中">生产中</option>
                        <option value="已完成">已完成</option>
                        <option value="已确认">已确认</option>
                        <option value="已发货">已发货</option>
                    </select>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">质量要求</label>
                <div class="layui-input-inline">
                    <select name="dQualityRequirement">
                        <option value="">请选择质量要求</option>
                        <option value="AAA">AAA</option>
                        <option value="AA">AA</option>
                        <option value="A">A</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">跟单</label>
                <div class="layui-input-inline">
                    <input type="text" name="dOrderFollower" placeholder="请输入跟单人员" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">业务员</label>
                <div class="layui-input-inline">
                    <input type="text" name="dSalesperson" placeholder="请输入业务员" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="dRemarks" placeholder="请输入备注内容" class="layui-textarea"></textarea>
            </div>
        </div>
        
        <!-- 产品明细区域 -->
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>销售订单产品明细信息</legend>
        </fieldset>
        
        <div class="layui-btn-group" style="margin-bottom: 10px;">
            <button type="button" class="layui-btn layui-btn-sm" id="addProductBtn">
                <i class="layui-icon layui-icon-add-1"></i> 添加
            </button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" id="deleteProductBtn">
                <i class="layui-icon layui-icon-delete"></i> 删除
            </button>
        </div>
        
        <div class="layui-table-body">
            <table class="layui-table" id="productTable" lay-filter="productTable">
                <thead>
                    <tr>
                        <th style="width: 40px;"><input type="checkbox" lay-filter="productCheckAll" lay-skin="primary"></th>
                        <th style="width: 60px;">序号</th>
                        <th>产品编码</th>
                        <th>产品名称</th>
                        <th>规格</th>
                        <th>材质</th>
                        <th>颜色</th>
                        <th>订货数量</th>
                        <th>单位</th>
                        <th>单价</th>
                        <th>金额</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="productTableBody">
                    <!-- 产品明细列表将通过JavaScript动态添加 -->
                </tbody>
            </table>
        </div>
    </form>
</div>

<!-- 添加产品明细对话框 -->
<div id="addProductDialog" style="display: none; padding: 20px;">
    <form class="layui-form" id="productForm" lay-filter="productForm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">产品编码</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="productCode" id="productCode" placeholder="请选择产品" autocomplete="off" class="layui-input" readonly>
                </div>
                <div class="layui-form-mid">
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="selectProductBtn">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">产品名称</label>
                <div class="layui-input-inline" style="width: 300px;">
                    <input type="text" name="productName" placeholder="产品名称" autocomplete="off" class="layui-input" readonly>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">规格</label>
                <div class="layui-input-inline">
                    <input type="text" name="specification" placeholder="规格" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">材质</label>
                <div class="layui-input-inline">
                    <input type="text" name="material" placeholder="材质" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">颜色</label>
                <div class="layui-input-inline">
                    <select name="color" id="productColor">
                        <option value="">请选择颜色</option>
                        <option value="红色">红色</option>
                        <option value="蓝色">蓝色</option>
                        <option value="绿色">绿色</option>
                        <option value="黄色">黄色</option>
                        <option value="黑色">黑色</option>
                        <option value="白色">白色</option>
                        <option value="其他">其他</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订货数量</label>
                <div class="layui-input-inline">
                    <input type="number" name="quantity" placeholder="订货数量" autocomplete="off" class="layui-input" lay-verify="required|number" value="1" min="1">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">单位</label>
                <div class="layui-input-inline">
                    <select name="unit" id="productUnit">
                        <option value="">请选择单位</option>
                        <option value="个">个</option>
                        <option value="件">件</option>
                        <option value="套">套</option>
                        <option value="kg">kg</option>
                        <option value="吨">吨</option>
                        <option value="米">米</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">单价</label>
                <div class="layui-input-inline">
                    <input type="number" name="price" placeholder="单价" autocomplete="off" class="layui-input" lay-verify="required|number" step="0.01" min="0">
                </div>
            </div>
        </div>
    </form>
</div>

<script>
layui.use(['table', 'form', 'laydate', 'layer'], function(){
    var table = layui.table,
        form = layui.form,
        laydate = layui.laydate,
        layer = layui.layer,
        $ = layui.jquery;
    
    // 初始化日期选择器
    laydate.render({
        elem: '#orderDateStart'
    });
    
    laydate.render({
        elem: '#orderDateEnd'
    });
    
    laydate.render({
        elem: '#deliveryDateStart'
    });
    
    laydate.render({
        elem: '#deliveryDateEnd'
    });
    
    // 初始化表格
    table.render({
        elem: '#salesOrderTable',
        url: '/OrderManagement/SalesOrder/GetSalesOrders',
        toolbar: '#toolbarTpl',
        defaultToolbar: ['filter', 'exports', 'print'],
        page: true,
        limit: 10,
        limits: [10, 20, 30, 50, 100],
        even: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'dOrderCode', title: '订单编码', width: 140, sort: true},
            {field: 'dOrderName', title: '订单名称', width: 180},
            {field: 'dOrderCategory', title: '订单类别', width: 100, templet: '#statusTpl'},
            {field: 'dCustomerCode', title: '客户编码', width: 120},
            {field: 'dCustomerName', title: '客户名称', width: 150},
            {field: 'dCustomerPONumber', title: '客户PO号', width: 120},
            {field: 'dOrderDate', title: '订货日期', width: 110, sort: true, templet: function(d){
                return d.dOrderDate ? layui.util.toDateString(new Date(d.dOrderDate), 'yyyy-MM-dd') : '';
            }},
            {field: 'deliveryDate', title: '交货日期', width: 110},
            {field: 'dCurrency', title: '币别', width: 80},
            {field: 'dSettlementMethod', title: '结账方式', width: 100},
            {field: 'dOrderFollower', title: '跟单', width: 90},
            {field: 'dSalesperson', title: '业务员', width: 90},
            {field: 'dQualityRequirement', title: '质量要求', width: 90},
            {field: 'dRemarks', title: '备注', width: 150},
            {fixed: 'right', title: '操作', toolbar: '#operationTpl', width: 100}
        ]],
        response: {
            statusCode: 0
        },
        parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.count,
                "data": res.data
            };
        },
        text: {
            none: '暂无相关数据'
        }
    });
    
    // 搜索按钮点击事件
    form.on('submit(searchBtn)', function(data){
        // 执行搜索重载
        table.reload('salesOrderTable', {
            page: {
                curr: 1 // 重新从第 1 页开始
            },
            where: data.field // 搜索的字段
        });
        return false; // 阻止表单跳转
    });
    
    // 表格工具条事件
    table.on('tool(salesOrderTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            // 跳转到编辑页面
            window.location.href = '/OrderManagement/SalesOrder/Edit?id=' + data.id;
        } else if(obj.event === 'del'){
            layer.confirm('确认要删除此订单吗？', {icon: 3, title:'提示'}, function(index){
                // 执行删除操作
                $.ajax({
                    url: '/OrderManagement/SalesOrder/Delete/' + data.id,
                    type: 'POST',
                    success: function(res){
                        if(res.code === 0){
                            layer.msg('删除成功', {icon: 1});
                            obj.del(); // 删除对应行（tr）的DOM结构
                        } else {
                            layer.msg('删除失败：' + res.msg, {icon: 2});
                        }
                    },
                    error: function(){
                        layer.msg('服务器错误，请稍后重试', {icon: 2});
                    }
                });
                layer.close(index);
            });
        }
    });
    
    // 重写新增按钮点击事件
    $('#addBtn').on('click', function(){
        // 打开添加销售订单对话框
        layer.open({
            type: 1,
            title: '添加销售订单',
            area: ['980px', '80%'],
            content: $('#addOrderDialog'),
            maxmin: true,
            btn: ['保存', '取消'],
            yes: function(index, layero){
                // 提交表单数据
                var formData = form.val('orderForm');
                
                // 获取产品明细数据
                var products = [];
                $('#productTableBody tr').each(function(){
                    var $row = $(this);
                    var product = {
                        productCode: $row.find('[name="productCode"]').val(),
                        productName: $row.find('[name="productName"]').val(),
                        specification: $row.find('[name="specification"]').val(),
                        material: $row.find('[name="material"]').val(),
                        color: $row.find('[name="color"]').val(),
                        quantity: parseFloat($row.find('[name="quantity"]').val()) || 0,
                        unit: $row.find('[name="unit"]').val(),
                        price: parseFloat($row.find('[name="price"]').val()) || 0,
                        amount: parseFloat($row.find('[name="amount"]').val()) || 0
                    };
                    products.push(product);
                });
                
                // 添加产品明细到表单数据
                formData.products = products;
                
                // 表单验证
                if (!formData.dOrderCode && !$('input[name="autoGenerate"]').prop('checked')) {
                    layer.msg('请输入订单编码或选择自动生成', {icon: 2});
                    return;
                }
                
                if (!formData.dCustomerName) {
                    layer.msg('请选择客户', {icon: 2});
                    return;
                }
                
                if (!formData.dOrderDate) {
                    layer.msg('请选择订货日期', {icon: 2});
                    return;
                }
                
                if (!formData.deliveryDate) {
                    layer.msg('请选择交货日期', {icon: 2});
                    return;
                }
                
                if (!formData.dOrderCategory) {
                    layer.msg('请选择订单类别', {icon: 2});
                    return;
                }
                
                // 如果没有产品明细，提示用户
                if (products.length === 0) {
                    layer.confirm('当前没有添加任何产品明细，确定要继续保存吗？', {icon: 3, title:'提示'}, function(confirmIndex){
                        // 用户确认后提交
                        submitOrderData(formData, index);
                        layer.close(confirmIndex);
                    });
                } else {
                    // 直接提交
                    submitOrderData(formData, index);
                }
            },
            success: function(layero, index){
                // 初始化日期选择器
                laydate.render({
                    elem: '#orderDate',
                    value: new Date()
                });
                
                laydate.render({
                    elem: '#deliveryDate'
                });
                
                // 自动生成订单编码处理
                form.on('checkbox(autoGenerate)', function(data){
                    var $orderCode = $('input[name="dOrderCode"]');
                    if(data.elem.checked){
                        $orderCode.val('自动生成').attr('readonly', true).removeAttr('lay-verify');
                    } else {
                        $orderCode.val('').removeAttr('readonly').attr('lay-verify', 'required');
                    }
                });
                
                // 重新渲染表单
                form.render();
            }
        });
    });
    
    // 提交订单数据到服务器
    function submitOrderData(formData, layerIndex) {
        // 字段名映射：将前端字段名转换为后端DTO属性名
        var mappedData = {
            dOrderCode: formData.dOrderCode,
            dOrderName: formData.dOrderName,
            dCustomerCode: formData.dCustomerCode,
            dCustomerName: formData.dCustomerName,
            dCustomerPONumber: formData.dCustomerPONumber,
            dOrderDate: formData.dOrderDate,
            deliveryDate: formData.deliveryDate,
            dCurrency: formData.dCurrency,
            dSettlementMethod: formData.dSettlementMethod,
            dOrderCategory: formData.dOrderCategory, // 订单类别
            dOrderFollower: formData.dOrderFollower,
            dSalesperson: formData.dSalesperson,
            dQualityRequirement: formData.dQualityRequirement,
            dRemarks: formData.dRemarks
        };
        
        // 确保产品明细数据正确格式化
        if (formData.products && formData.products.length > 0) {
            formData.products.forEach(function(product) {
                // 确保数值类型正确
                product.quantity = parseFloat(product.quantity) || 0;
                product.price = parseFloat(product.price) || 0;
                product.amount = parseFloat(product.amount) || 0;
            });
            mappedData.products = formData.products;
        } else {
            mappedData.products = [];
        }
        
        // 处理自动生成订单编码
        if ($('input[name="autoGenerate"]').prop('checked')) {
            mappedData.dOrderCode = ''; // 后端会自动生成
        }
        
        // 打印提交的数据，便于调试
        console.log('提交的数据:', JSON.stringify(mappedData));
        
        // 提交到后端
        $.ajax({
            url: '/OrderManagement/SalesOrder/Add',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(mappedData),
            success: function(res){
                if(res.code === 0){
                    layer.msg('添加成功', {icon: 1});
                    layer.close(layerIndex);
                    // 重载表格
                    table.reload('salesOrderTable');
                } else {
                    layer.msg('添加失败：' + res.msg, {icon: 2});
                }
            },
            error: function(xhr, status, error){
                console.error('保存失败:', xhr.responseText);
                layer.msg('服务器错误，请稍后重试：' + error, {icon: 2});
            }
        });
    }
    
    // 添加产品明细
    $('#addProductBtn').on('click', function(){
        layer.open({
            type: 1,
            title: '添加产品明细',
            area: ['600px', '500px'],
            content: $('#addProductDialog'),
            btn: ['确定', '取消'],
            yes: function(index, layero){
                // 获取产品表单数据
                var productData = form.val('productForm');
                
                // 验证必填项
                if(!productData.productCode || !productData.quantity || !productData.price){
                    layer.msg('请填写完整的产品信息', {icon: 0});
                    return;
                }
                
                // 计算金额
                var amount = (parseFloat(productData.quantity) * parseFloat(productData.price)).toFixed(2);
                
                // 添加到产品明细表格
                var rowCount = $('#productTableBody tr').length + 1;
                var rowHtml = '<tr>' +
                    '<td><input type="checkbox" lay-skin="primary" lay-filter="productCheck"></td>' +
                    '<td>' + rowCount + '</td>' +
                    '<td><input type="hidden" name="productCode" value="' + productData.productCode + '">' + productData.productCode + '</td>' +
                    '<td><input type="hidden" name="productName" value="' + productData.productName + '">' + productData.productName + '</td>' +
                    '<td><input type="hidden" name="specification" value="' + (productData.specification || '') + '">' + (productData.specification || '') + '</td>' +
                    '<td><input type="hidden" name="material" value="' + (productData.material || '') + '">' + (productData.material || '') + '</td>' +
                    '<td><input type="hidden" name="color" value="' + (productData.color || '') + '">' + (productData.color || '') + '</td>' +
                    '<td><input type="hidden" name="quantity" value="' + productData.quantity + '">' + productData.quantity + '</td>' +
                    '<td><input type="hidden" name="unit" value="' + (productData.unit || '') + '">' + (productData.unit || '') + '</td>' +
                    '<td><input type="hidden" name="price" value="' + productData.price + '">' + productData.price + '</td>' +
                    '<td><input type="hidden" name="amount" value="' + amount + '">' + amount + '</td>' +
                    '<td>' +
                        '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete-product">' +
                            '<i class="layui-icon layui-icon-delete"></i>' +
                        '</button>' +
                    '</td>' +
                '</tr>';
                
                $('#productTableBody').append(rowHtml);
                
                // 重新渲染
                form.render('checkbox');
                
                // 清空表单
                document.getElementById('productForm').reset();
                form.render();
                
                // 关闭对话框
                layer.close(index);
            },
            success: function(layero, index){
                // 重置表单
                document.getElementById('productForm').reset();
                form.render();
            }
        });
    });
    
    // 删除选中的产品明细
    $('#deleteProductBtn').on('click', function(){
        var $checkedRows = $('#productTableBody input[type="checkbox"]:checked').parents('tr');
        if($checkedRows.length === 0){
            layer.msg('请至少选择一条记录', {icon: 0});
            return;
        }
        
        layer.confirm('确认要删除选中的 ' + $checkedRows.length + ' 条产品明细吗？', {icon: 3, title:'提示'}, function(index){
            // 删除选中的行
            $checkedRows.remove();
            
            // 重新编号
            $('#productTableBody tr').each(function(i){
                $(this).find('td:eq(1)').text(i + 1);
            });
            
            layer.close(index);
        });
    });
    
    // 动态绑定删除单个产品明细的事件
    $(document).on('click', '.delete-product', function(){
        var $row = $(this).parents('tr');
        
        layer.confirm('确认要删除这条产品明细吗？', {icon: 3, title:'提示'}, function(index){
            // 删除当前行
            $row.remove();
            
            // 重新编号
            $('#productTableBody tr').each(function(i){
                $(this).find('td:eq(1)').text(i + 1);
            });
            
            layer.close(index);
        });
    });
    
    // 选择产品按钮点击事件
    $('#selectProductBtn').on('click', function(){
        // 此处应打开产品选择对话框，实际实现需要根据系统中的产品管理功能来完成
        // 简化示例：直接设置一些测试数据
        layer.msg('这里应弹出产品选择窗口，目前使用模拟数据', {icon: 0});
        
        setTimeout(function(){
            // 模拟选中了一个产品
            form.val('productForm', {
                'productCode': 'P001',
                'productName': '测试产品',
                'specification': '标准型',
                'material': '铝合金',
                'color': '银色',
                'unit': '个',
                'price': '100.00'
            });
            form.render('select');
        }, 500);
    });
    
    // 选择客户按钮点击事件
    $('#selectCustomerBtn').on('click', function(){
        // 此处应打开客户选择对话框，实际实现需要根据系统中的客户管理功能来完成
        // 简化示例：直接设置一些测试数据
        layer.msg('这里应弹出客户选择窗口，目前使用模拟数据', {icon: 0});
        
        setTimeout(function(){
            // 模拟选中了一个客户
            form.val('orderForm', {
                'dCustomerCode': 'C00002',
                'dCustomerName': '金融街'
            });
        }, 500);
    });
});
</script> 